//
//  ScanSuccessViewController.m
//  HYQRCodeDemo
//
//  Created by leimo on 2017/6/1.
//  Copyright © 2017年 huyong. All rights reserved.
//

#import "ScanSuccessViewController.h"

@interface ScanSuccessViewController () <WKUIDelegate,WKNavigationDelegate>

/** webView */
@property (nonatomic,strong)  WKWebView *webView;
/** progress */
@property (nonatomic,strong)  UIProgressView *progressView;
/** resultView */
@property (nonatomic,strong)  UIView *resultView;

@end

@implementation ScanSuccessViewController

- (void)dealloc{
    
    if (_webView) {
        
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.url) {
        
        [self.view addSubview:self.webView];
        [self.view addSubview:self.progressView];
        [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    }
    else{
        
        [self.view addSubview:self.resultView];
    }
}

#pragma mark - 监听加载进度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if (object == self.webView &&  [keyPath isEqualToString:@"estimatedProgress"]) {
        
        CGFloat newProgress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newProgress == 1) {
            self.progressView.hidden = YES;
        }
        else{
            self.progressView.hidden = NO;
            self.progressView.progress = newProgress;
        }
    }
}

#pragma mark - webViewDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        self.title = result;
    }];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    
    NSLog(@"%@",error);
}

#pragma mark - lazyload
- (WKWebView *)webView{
    
    if (!_webView) {
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - KNAV_HEIGHT)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
        
        //        //创建WKWebView的配置对象
        //        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        //        //设置configuration对象的preferences属性的信息
        //        WKPreferences *preference = [[WKPreferences alloc] init];
        //        configuration.preferences = preference;
        //        //是否允许与JS交互，默认YES
        //        preference.javaScriptEnabled = YES;
        //        //通过JS与WebView内容交互
        //        configuration.userContentController = [[WKUserContentController alloc] init];
        //        [configuration.userContentController addScriptMessageHandler:self name:@"callback"];
    }
    return _webView;
}

- (UIView *)resultView{
    
    if (!_resultView) {
        
        _resultView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 140, 200, 300)];
        label.text = [NSString stringWithFormat:@"你扫描的结果如下:\n%@",self.text];
        label.font = [UIFont systemFontOfSize:16];
        label.numberOfLines = 0;
        [_resultView addSubview:label];
    }
    return _resultView;
}

- (UIProgressView *)progressView{
    
    if (!_progressView) {
        
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
        _progressView.backgroundColor = [UIColor whiteColor];
        _progressView.progress = 0;
        _progressView.progressTintColor = [UIColor blueColor];
    }
    return _progressView;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}


@end
